
import { setDocumentTitle } from '@/utils/common.methods';
import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from 'vue-router';
import { MessagePlugin } from "tdesign-vue-next";
import { appContext } from '@/models/app.context';
import { appInfo } from '@/tools/hope/@types/config';
import { appNotify, NotifyOption, NotifyType } from '@/models/app.notify';
import { UserRole } from '@/models/user';
import { appRef } from '@/models/app.ref';

// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
const routes: Readonly<RouteRecordRaw[]> = [
  { path: '/', redirect: 'tools' },
  {
    name: "note",
    path: '/note',
    children: [
      { path: "", component: () => import('@/views/note/Index.vue'), meta: { transitionName: 'slide-left', title: "便签", isLogin: false } },
      { path: "detail", component: () => import('@/views/note/Detail.vue'), meta: { transitionName: 'slide-left', title: "便签详情", isLogin: false, isFullScreen: true } },
    ],
  },
  { name: "mine", path: '/mine', component: () => import('@/views/mine/Index.vue'), meta: { transitionName: 'slide-left', title: "我的", isLogin: true } },
  {
    name: "tools",
    path: '/tools',
    children: [
      { path: "", component: () => import('@/views/tool/Index.vue'), meta: { transitionName: 'slide-left', title: "工具", isLogin: false } },
      { path: "base64", component: () => import('@/components/tool/Base64.vue'), meta: { transitionName: 'slide-left', title: "base64", isLogin: false } },
      { path: "json-pretty", component: () => import('@/components/tool/JSONPretty.vue'), meta: { transitionName: 'slide-left', title: "json-pretty", isLogin: false } },
      { path: "timestamp", component: () => import('@/components/tool/TimeStamp.vue'), meta: { transitionName: 'slide-left', title: "timestamp", isLogin: false } },
    ]
  },
  { name: "docs", path: '/docs', component: () => import('@/views/doc/Index.vue'), meta: { transitionName: 'slide-left', title: "文档", isLogin: false } },
  { name: "code", path: '/code', component: () => import('@/views/code/Index.vue'), meta: { transitionName: 'slide-left', title: "代码分享", isLogin: false } },

  // admin
  {
    name: "admin", path: '/admin',
    component: () => import('@/views/admin/Index.vue'), meta: { transitionName: 'slide-left', title: "admin", isLogin: true, isFullScreen: true, role: UserRole.Root },
    children: [
      { path: '', redirect: '/admin/user' },
      { path: "user", component: () => import('@/views/admin/User.vue'), meta: { transitionName: 'slide-left', title: "用户列表", isLogin: true, isFullScreen: true, role: UserRole.Root } },
      { path: "access-log", component: () => import('@/views/admin/AccessLog.vue'), meta: { transitionName: 'slide-left', title: "访问监控", isLogin: true, isFullScreen: true, role: UserRole.Root } },
      { path: "note", component: () => import('@/views/admin/Note.vue'), meta: { transitionName: 'slide-left', title: "便签管理", isLogin: true, isFullScreen: true, role: UserRole.Root } },
    ]

  },




  // sign in
  { name: "login", path: '/login', component: () => import('@/views/login/SinIn.vue'), meta: { transitionName: 'slide-left', title: "登录", isLogin: false, isFullScreen: true } },
  { name: "register", path: '/register', component: () => import('@/views/login/Register.vue'), meta: { transitionName: 'slide-left', title: "注册", isLogin: false, isFullScreen: true } },
  { name: "forgotPassword", path: '/forgotPassword', component: () => import('@/views/login/ForgotPassword.vue'), meta: { transitionName: 'slide-left', title: "找回密码", isLogin: false, isFullScreen: true } },
]



//  创建路由实例并传递 `routes` 配置
const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  // history: createWebHistory(),
  history: createWebHashHistory(),
  routes, // `routes: routes` 的缩写
})
// //beforeEach是router的钩子函数，在进入路由前执行
router.beforeEach((to, from, next) => {
  //判断是否有标题 
  if (to?.meta?.title) { setDocumentTitle(to.meta.title + ' | ' + appInfo.name as string) }
  if (to?.meta?.isLogin || to?.meta?.role) { // 该路由 是否需要登录
    if (!appContext.global?.token) {
      router.replace("/login");
      MessagePlugin("error", "请先登录");
      return;
    }
    if (appRef.user?.data?.role > to?.meta?.role) {
      router.replace("/");
      return;
    }
  }
  if (to?.meta?.isFullScreen) { // 该路由 是否需要'全屏'  (没有 header or footer)
    appNotify.send(NotifyType.UpdatedHeaderFooter, NotifyOption.Updated, 'false')
  } else {
    appNotify.send(NotifyType.UpdatedHeaderFooter, NotifyOption.Updated, 'true')
  }
  appNotify.send(NotifyType.RouterChange, NotifyOption.Empty, to.path)
  next(); //执行进入路由，如果不写就不会进入目标页
})


export default router;
// 现在，应用已经启动了！